// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import Client from '@api/user/api/SysUserApi'

import ComClass from '@api/ComQuery'
import ListClass from '@api/user/model/SysUserDto'
import DelClass from '@api/user/model/SysUserDeleteReq'
import DownloadClass from '@api/user/model/TemplateDownLoadReq'
import UploadClass from '@api/user/model/SysUserExcelImportReq'
import UpProgressClass from '@api/user/model/SysUserExcelImportProcessReq'

const AddClass = ListClass
const EditClass = ListClass

const CLIENT = CORE.grpcCLIENT(Client)
const COM = [ComClass, 'comQuery']

// ============================== 导出远程接口 ============================== //

export default {
  // —————————————————————————————— 列表 —————————————————————————————— //

  async getList(param, TIP = '查询列表') {
    const DTO = CORE.grpcDTO(ListClass, param, COM)
    const resp = await CORE.grpc(TIP, CLIENT, 'sysUserQuery', DTO)
    return [resp.comQuery.totalCount, resp.content || []]
  },
  transList(arr) {
    arr.forEach((item) => {
      item.organIdStr = item.organId.join()
      item.validDate = item.expireTime === -1 ? 1 : 2 // 有效期
      item.validNum = item.loginLimit === -1 ? 1 : 2 // 登录次数
      item.someDate = item.expireTime === -1 ? undefined : item.expireTime // 指定日期
      item.someNum = item.loginLimit === -1 ? undefined : item.loginLimit === -2 ? 0 : item.loginLimit // 指定次数
    })
    return arr
  },

  // —————————————————————————————— 增 —————————————————————————————— //

  async transAdd(one) {
    if (!one.pwd || !one.repeatPwd) return one

    const pwd = await CORE.rsa(one.pwd)
    const repeatPwd = await CORE.rsa(one.repeatPwd)
    return { ...one, pwd, repeatPwd }
  },
  async add(one, TIP = '新增') {
    const DTO = CORE.grpcDTO(AddClass, one)
    await CORE.grpc(TIP, CLIENT, 'sysUserSave', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 改 —————————————————————————————— //

  transEditToAdd: true,
  async edit(one, TIP = '修改') {
    const DTO = CORE.grpcDTO(EditClass, one)
    await CORE.grpc(TIP, CLIENT, 'sysUserUpdate', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 删 —————————————————————————————— //

  async del({ id }, TIP = '删除') {
    const DTO = CORE.grpcDTO(DelClass, { ids: id })
    await CORE.grpc(TIP, CLIENT, 'sysUserDelete', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 下载 —————————————————————————————— //

  async download(downParam, TIP = '下载模板') {
    const DTO = CORE.grpcDTO(DownloadClass, downParam)
    await CORE.grpcDownload(TIP, CLIENT, 'templateDownLoad', DTO, null, '用户导入模板.xlsx')
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 上传 —————————————————————————————— //

  async upload(upParam, TIP = '上传文件') {
    const DTO = CORE.grpcDTO(UploadClass, upParam)
    const resp = await CORE.grpc(TIP, CLIENT, 'sysUserExcelImport', DTO)
    return resp.taskId
  },

  // —————————————————————————————— 上传进度 —————————————————————————————— //

  async upProgress(upProgress, TIP = '查询上传进度') {
    const DTO = CORE.grpcDTO(UpProgressClass, upProgress)
    const resp = await CORE.grpc(TIP, CLIENT, 'sysUserExcelImportProcess', DTO)
    return resp
  },
}
